package com.hmjk.health;

import com.hmjk.health.home.HomeActivity;
import com.hmjk.health.setting.system.UserManualActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class IndexActivity extends Activity implements OnClickListener{
	private final String TAG = IndexActivity.class.getSimpleName();
	public static final String ENTER_LOCATION = "ENTER_LOCATION";
	public static final String ENTER_HEALTH = "ENTER_HEALTH";
	public static final String ENTER_STEPCOUNTER = "ENTER_STEPCOUNTER";
	private HealthApp mApp = null;
	//private String backto;
	private boolean destroyAD = true;
	private boolean destroyData = true;
	private Toast mQuitToast = null;
	private Animation animBreath = null;
	private Animation animCycle = null;
	private Animation animRun = null;

	private void logd(String info){
		Log.d(TAG,info);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mApp = (HealthApp)getApplication();
		
		setContentView(R.layout.index_activity);
		
		TextView help = (TextView) findViewById(R.id.helpmanual);
		String helpstr = getResources().getString(R.string.user_manual);
		help.setText(Html.fromHtml(helpstr));
		help.setOnClickListener(this);
		
		View enterLocation = findViewById(R.id.loca_enter);
		enterLocation.setOnClickListener(this);
		
		View enterHealth = findViewById(R.id.health_enter);
		enterHealth.setOnClickListener(this);
		
		View enterSport = findViewById(R.id.sport_enter);
		enterSport.setOnClickListener(this);
		
		animBreath = AnimationUtils.loadAnimation(this, R.anim.breath);
		animCycle = AnimationUtils.loadAnimation(this, R.anim.rotate360);
		animRun = AnimationUtils.loadAnimation(this, R.anim.l_to_r_in);
		
		/*used by location icon*/
		animCycle.setAnimationListener(new AnimationListener(){

			@Override
			public void onAnimationEnd(Animation arg0) {
				destroyAD = false;
				destroyData = false;
				
				Intent i = new Intent(IndexActivity.this,HomeActivity.class);
				i.putExtra("enter_type", ENTER_LOCATION);
				startActivity(i);
				mApp.setlastVisit(ENTER_LOCATION);
				finish();	
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {}

			@Override
			public void onAnimationStart(Animation arg0) {}
			
		});
		
		/*used by health icon*/
		animBreath.setAnimationListener(new AnimationListener(){

			@Override
			public void onAnimationEnd(Animation arg0) {
				destroyAD = false;
				destroyData = false;
				Intent i = new Intent(IndexActivity.this,HomeActivity.class);
				i.putExtra("enter_type", ENTER_HEALTH);
				startActivity(i);
				mApp.setlastVisit(ENTER_HEALTH);
				finish();
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {}

			@Override
			public void onAnimationStart(Animation arg0) {}
			
		});
		
		/*used by sport icon*/
		animRun.setAnimationListener(new AnimationListener(){

			@Override
			public void onAnimationEnd(Animation arg0) {
				//destroyAD = false;
				//destroyData = false;
				
				//TODO:enter step counter activity
				//mApp.setlastVisit(ENTER_STEPCOUNTER);
				//finish();
				View enterSport = findViewById(R.id.sport_enter);
				showPopInfo("Comming soon..", enterSport);
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {}

			@Override
			public void onAnimationStart(Animation arg0) {}
			
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){
	   	
	   	if(keyCode == KeyEvent.KEYCODE_BACK){
	   		if(mQuitToast == null){
	   			mQuitToast = Toast.makeText(this, R.string.quit_hint, Toast.LENGTH_LONG);
	   			mQuitToast.show();
	   			mHandler.removeMessages(MSG_DOUBLE_BACK_CHECK);
	   			Message m = mHandler.obtainMessage(MSG_DOUBLE_BACK_CHECK);
	   			mHandler.sendMessageDelayed(m, 3000);
	   		}else{
	   			mQuitToast.cancel();
	   			mQuitToast = null;
	   			mApp.releaseAllAD();
	   			mApp.releaseAllData();
	   			finish();
	   		}
	   		return true;
	   		
	   	}
	   	return super.onKeyDown(keyCode,event);
    }
	
	private final int MSG_DOUBLE_BACK_CHECK = 801; 
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			switch(msg.what){
			case MSG_DOUBLE_BACK_CHECK:{
				if(mQuitToast != null){
					mQuitToast.cancel();
					mQuitToast = null;
				}
			}
			break;
			}
		}
	};
	
	@Override
	public void onResume(){
		super.onResume();
		mApp.setlastVisit("none");
	}
	
	@Override
	public void onDestroy(){
		logd("onDestroy");
		
		if(destroyAD){
			mApp.releaseAllAD();
		}
		if(destroyData){
			mApp.releaseAllData();
		}
		
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.helpmanual:{
			Intent i = new Intent(this,UserManualActivity.class);
			startActivity(i);
		}break;
		
		case R.id.loca_enter:{
			ImageView icon = (ImageView) ((LinearLayout)v).getChildAt(0);
			icon.startAnimation(animCycle);
		}break;

		case R.id.health_enter:{
			ImageView icon = (ImageView) ((LinearLayout)v).getChildAt(0);
			icon.startAnimation(animBreath);
		}break;

		case R.id.sport_enter:{
			ImageView icon = (ImageView) ((LinearLayout)v).getChildAt(0);
			icon.startAnimation(animRun);
		}break;
		}
	}
	
	/*private void showHelpInfo(){
		helpInfoVer = mApp.getHelpVersion(TAG);
		if(helpInfoVer < 1){
			LayoutInflater inflater = LayoutInflater.from(this);
			View helpview = inflater.inflate(R.layout.help_layout, null);
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(     
					RelativeLayout.LayoutParams.MATCH_PARENT,     
					RelativeLayout.LayoutParams.MATCH_PARENT);
			addContentView(helpview, params);
		}
	}*/
	
	private PopupWindow infoWin = null;
	private View infoView = null;
	
	private void showPopInfo(String show, View anchor){
		if((infoWin != null) && infoWin.isShowing()){
			infoWin.dismiss();
		}else{
			if(infoWin == null){
				LayoutInflater inflater = LayoutInflater.from(this);
				infoView = inflater.inflate(R.layout.pop_text_info, null);
				infoWin = new PopupWindow(infoView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
				infoWin.setTouchable(true);
				infoWin.setOutsideTouchable(true);
				infoWin.setBackgroundDrawable(new ColorDrawable(0x00000000));
				infoWin.setOnDismissListener(new PopupWindow.OnDismissListener(){
	
					@Override
					public void onDismiss() {
						infoWin = null;
						infoView = null;
					}
					
				});
			}
		
			TextView tv = (TextView) infoView.findViewById(R.id.pop_info);
			tv.setText(show);
			infoWin.showAsDropDown(anchor);
		}
	}
}
